<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="nav">
    五子棋对战
</div>
<div class="login-container">
    <!-- 登录时的对话框 -->
    <div class="login-dialog">
        <h2>注册</h2>
        <!-- 一行 -->
        <div class="row">
            <span>用户名</span>
            <input type="text" id="userName">
        </div>
        <!-- 一行 -->
        <div class="row">
            <span>密码</span>
            <input type="password" id="password">
        </div>
        <!-- 提交按钮 -->
        <div class="row">
            <button id="submit">提交</button>
        </div>
        <div class="row">
            <button id="login">去登录</button>
        </div>

    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    let loginButton = document.querySelector('#login');
    let usernameInput = document.querySelector('#userName');
    let passwordInput = document.querySelector('#password');
    let submitButton = document.querySelector('#submit');

    loginButton.onclick = function () {
        location.replace('/login.html');
    }
    submitButton.onclick = function () {
        $.ajax({
            type: "post",
            url: "/user/register",
            data: {
                userName: usernameInput.value,
                password: passwordInput.value
            },
            success: function(event) {
                if (event != null && event.code == 200) {
                    let result = event.data;
                    if (result != null || result.flag == true) {
                        // 注册成功
                        alert(result.message);
                        location.replace("/login.html");
                    } else {
                        alert(result.message);
                    }
                }else {
                    alert(event.message);
                }
            },
            error: function() {
                alert("注册失败!");
            }
        });
    }

</script>
</body>
</html>